<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>wenjuandiaocha</title>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script src="/js/vue.js"></script>
    <!-- 引入样式element-->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
            font-size: 14px;
            /* 可以消除边框对长宽的影响 */
        }

        #container {
            position: fixed;
            top: 0;
            right: 0;
            left: 0;
            bottom: 0;
            background: url("/img/bacg.jpg") no-repeat center center;
        }

        .forms {
            width: 600px;
            margin: 100px auto;
            border: 1px solid rgba(0, 0, 0, 0.2);
            padding: 20px;
            border-radius: 10px;
            box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.2);
            background-color: #fff;
        }

        .forms .logo {
            width: 100%;
            height: 100px;
        }

        .forms .logo img {
            height: 60%;
        }

        .forms .logo span {
            line-height: 60px;
            font-size: 25px;
            float: right;
            margin-right: 40%;
        }

        .forms form div button {
            margin-left: 100px;
            margin-right: 30px;
        }

        a {
            color: rgba(0, 0, 0, 0.5);
            text-decoration: none;
        }

        a:hover {
            color: rgba(0, 0, 0, 0.8);
        }
        .yiyou{
           text-align:right;
        }
    </style>
</head>

<body>
    <div id="container">
        <div class="forms">
            <div class="logo">
                <img src="/img/logo.png" alt="">
                <span>
                    用户注册
                </span>
            </div>
            <el-form id="regFrm" :status-icon=true :rules="rules" label-position="left" action="/users/add" method="post" ref="form"
                :model="form" label-width="130px">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="form.username" name="username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="form.password" name="password"></el-input>
                </el-form-item>
                <el-form-item label="再次输入密码" prop="password2">
                    <el-input type="password" v-model="form.password2"></el-input>
                </el-form-item>
                <el-form-item label="手机" prop="phone">
                    <el-input v-model="form.phone" name="phone"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="form.email" name="email"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-radio-group v-model="form.sex">
                        <el-radio label="1" name="sex">男</el-radio>
                        <el-radio label="2" name="sex">女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item>
                    <el-button native-type="submit" type="primary">立即创建</el-button>
                </el-form-item>
                <el-form-item class="yiyou">
                   <a href="/login">已有账号，立即登录</a>
                </el-form-item>
            </el-form>
        </div>
    </div>
</body>
</html>
<script>
    var regVm = new Vue({
        el: '#container',
        data: function () {
            var _this = this
            // 看用户名是否存在
            var chkExist = function (option, callback) {
                $.get('/chkExist', {
                    filed: option.filed,
                    value: option.value,
                    table: option.table,// 数据库中的表名称
                }, function (result) {
                    if (result.error == 0) {
                        callback()
                    } else {
                        callback('用户名已经存在')
                    }
                })
            }
            // 封装二次密码验证
            var chkPwd = function (value, callback) {
                if (value.trim() == '') {
                    callback(new Error('请输入确认密码'))
                } else if (value == _this.form.password) {
                    callback()
                } else {
                    callback(new Error('两次密码输入不一致'))
                }
            }
            return {
                form: {
                    username: '',
                    password: '',
                    password2: '',
                    phone: '',
                    email: '',
                    sex: '1'
                },
                rules: {
                    username: [
                        {
                            // 第三方自定义规则
                            validator: function (rule, value, callback) {
                                var usernamereg = /^[a-z]\w{1,17}$/
                                if (value.trim() == '') {
                                    callback(new Error("请输入用户名"))
                                } else if (!usernamereg.test(value)) {
                                    callback(new Error('以小写字母开始,请输入2到18个字符'))
                                } else {
                                    // 看用户名是否存在,,chkExist中括号内是参数
                                    chkExist({
                                        filed: 'username',
                                        value: value,
                                        table: 'usernode'
                                    }, callback)
                                }
                            },
                            trigger: 'blur'
                        }
                    ],
                    password: [
                        {
                            validator: function (rule, value, callback) {
                                var len = value.length
                                if (value.trim() == '') {
                                    callback(new Error("请输入密码"))
                                } else if (len < 6 || len > 18) {
                                    callback(new Error('请输入长度6-18个字符的密码'))
                                } else {
                                    regVm.$refs.form.validateField('password2')
                                    // validateField表示触发单个验证规则
                                    callback()
                                }
                            },
                            // 默认为change
                            // trigger: 'blur'
                        }
                    ],
                    password2: [
                        {
                            validator: function (rule, value, callback) {
                                chkPwd(value, callback)
                            }
                        }
                    ],
                    email: [
                        {
                            // 第三方自定义规则
                            validator: function (rule, value, callback) {
                                var emailgs = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/
                                if (value.trim() == '') {
                                    callback(new Error("请输入邮箱"))
                                } else if (!emailgs.test(value)) {
                                    callback(new Error('邮箱格式不正确'))
                                } else {
                                    // 看邮箱是否存在,,这里chkExist中括号内是传入的参数
                                    chkExist({
                                        filed: 'email',
                                        value: value,
                                        table: 'usernode'
                                    }, callback)
                                }
                            },
                            trigger: 'blur'
                        }
                    ],
                    phone: [
                        {
                            // 第三方自定义规则
                            validator: function (rule, value, callback) {
                                var phonegs = /^1[3,5,7,8,9]\d{9}$/
                                if (value.trim() == '') {
                                    callback(new Error("请输入手机号码"))
                                } else if (!phonegs.test(value)) {
                                    callback(new Error('手机号码格式不正确'))
                                } else {
                                    // 看手机号码是否存在,,这里chkExist中括号内是传入的参数
                                    chkExist({
                                        filed: 'phone',
                                        value: value,
                                        table: 'usernode'
                                    }, callback)
                                }
                            },
                            trigger: 'blur'
                        }
                    ],
                }
            }
        },
        methods: {
            regist: function () {
                this.$refs.form.validate(function (valid) {
                    if (valid) {
                        //如果valid为真，即验证全部通过时提交数据
                        regFrm.submit()
                    }
                })
            }
        }
    })







</script>